<?php

class Session extends CI_Controller{
	var $_user_table = 'pengguna';
	public function __construct()
	{
		parent::__construct();

		$this->load->database();
		$this->load->helper('url');
		$this->load->model('grid_model');
		$this->load->library('grocery_CRUD');
	}
	public function _example_output($output = null)
	{
		$this->load->view('master/grid',$output);
	}
	public function login()
	{ 
		//var_dump($this->LoginInfo->IsLoggedIn());
		

		if($this->LoginInfo->IsLoggedIn()){
			//die('REDIR');
			redirect('welcome/greeting');
			exit;
		}
		$data = array('err'=>'');
		if($this->input->post('do_login'))
		{
			$username = $this->input->post('username');
			$password = $this->input->post('password');
			
			//echo $this->LoginInfo->encrypt_password($password);

			if(!$this->LoginInfo->validate_password($password))
			{
				$data['err']= 'Username dan Password Anda salah.';//'Password harus berisi huruf dan angka.';
			}
			// else if(strlen($password) >= 6 )
			// {	
				$userInfo = $this->grid_model->get_user_info($username);

				//$userInfo
				if(!$userInfo)
				{
					$developer_config = $this->config->item('developer');

					if(is_array($developer_config['extend_user']))
					{

						if(count($developer_config['extend_user']) > 0 )
						{
							//print_r(count($developer_config['extend_user']));
							foreach ($developer_config['extend_user'] as $user) {
								if( $user['username'] == $username )
								{
									$userInfo = $user;
									break;
								}
							}
						}
					}
				}
				if($userInfo){

					if( trim($userInfo['password']) == $this->LoginInfo->encrypt_password($password))
					{	
						// ARE USER IN ENABLED STATE ?
						if($userInfo['status'] == 'Aktif')
						{	
							// USER MUST HAVE SESSION
							$this->LoginInfo->SetUserInfo($userInfo);
							$this->LoginInfo->LandingPage();
							return;
						}
						else
						{
							$data['err']= 'Akun tidak aktif.';
						}
					}
					else
					{
						$data['err']= 'Username dan Password Anda salah.';//'Password Anda salah.';
					}
				}else{
					$data['err']= 'Username dan Password Anda salah.';
					//return;
				}
			// }
			// else
			// {
			// 	$data['err']= 'Username dan Password Anda salah.';//'Password minimal 6 karakter.';
			// }
		}
		
		// $output = array('output'=> $this->load->view('_session_login',$data,TRUE));

		$this->load->view('session/login',$data);
		//die('LOGIN');
	}

	public function logout()
	{
		$this->LoginInfo->UnsetUserInfo();
		$this->LoginInfo->Redirect();
	}
	public function password()
	{
		# code...
	}
	public function guest()
	{
		# code...
		echo 'Welcome Guest';
	}
	public function profile()
	{
		# code...
	}
	public function state()
	{
		echo json_encode( array('status'=> $this->LoginInfo->IsLoggedIn()) );
	}

	public function islogin($echo=FALSE)
	{
		//$this->LoginInfo->IsLoggedIn();
	}
	
}